package role

import (
	"context"
	"strings"

	"mall/common/errorx"
	"mall/common/jwtx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRoleLogic {
	return &CreateRoleLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateRoleLogic) CreateRole(req *types.CreateRoleRequest) (resp *types.CreateRoleResponse, err error) {
	if len(strings.TrimSpace(req.Name)) == 0 {
		return nil, errorx.Error(errorx.ERROR_ROLE_NAME_REQUIRED, "")
	}

	adminId, _ := jwtx.GetUserId(l.ctx)

	_, err = l.svcCtx.BackendRpc.BackendCreateRole(l.ctx, &backendclient.CreateRoleRequest{
		Name:      req.Name,
		Note:      req.Note,
		Status:    req.Status,
		CreatedBy: adminId,
	})
	if err != nil {
		return
	}

	return &types.CreateRoleResponse{
		Status:  "ok",
		Message: "添加角色成功",
	}, nil
}
